<?php
session_start();

function success(){
    $result=[
        'error'=>'0',
        'message'=>'成功'
    ];
    return json_encode($result);
}

function error($msg){
    $data=[
        'error'=>'1',
        'message'=>$msg
    ];
    return json_encode($data);
}
$username=$_POST['username'];
$password=$_POST['password'];
$confirm=$_POST['confirm'];
$nickname=$_POST['nickname'];
$birthday=$_POST['birthday'];
$sex=$_POST['sex'];

$config = include '../config/db.php';
$db = new PDO($config['dsn'],$config['username'],$config['password']);
$statement=$db->prepare('select * from user where username=:username');
$statement->execute([':username'=>$username]);
$user=$statement->fetch();
if(empty($username)){
    die(error('用户不能为空！'));
}

if(empty($password)){
    die(error('密码不能为空！'));
}

if($password!=$confirm){
    die(error('密码不一致，请重新输入：'));
}
if(empty($nickname)){
    die(error('昵称不能为空！'));
}

if(empty($birthday)){
    die(error('出生日期不能为空！'));
}

if(empty($sex)){
    die(error('性别不能为空！'));
}

if(empty($user)){
    $statement = $db->prepare('insert into user(username,password,nickname,birthday,sex) values (:username,:password,:nickname,:birthday,:sex)');
    $result = $statement->execute([':username' => $username, ':password' => $password, ':nickname' => $nickname, ':birthday' => $birthday,':sex' => $sex]);
}
else{
    die(error('用户名已存在！'));
}
echo success();